package cn.mxh.pethspt.service.impl;

import cn.mxh.pethspt.dao.DingTotalDao;
import cn.mxh.pethspt.dao.impl.DingTotalDaoImpl;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.entity.DingTotal;
import cn.mxh.pethspt.service.DingTotalService;
import cn.mxh.pethspt.utils.DruidUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class DingTotalServiceImpl implements DingTotalService {
    DingTotalDao dingTotalDao = new DingTotalDaoImpl();
    /**
     * 添加订单总表
     *
     * @param dingTotal
     * @return
     */
    @Override
    public boolean add(DingTotal dingTotal) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = dingTotalDao.add(dingTotal);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 查询所有订单
     *
     * @return
     */
    @Override
    public List<DingTotal> getAll() throws Exception {
        Connection conn = null;
        List<DingTotal> dingTotalList = new ArrayList<>();
        try{
            conn = DruidUtils.getConnection();
            dingTotalList = dingTotalDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn);
        }
        return  dingTotalList;
    }
}
